package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillOilPriceService;
import cn.huiyunche.base.service.mappers.DWaybillOilPriceMapper;
import cn.huiyunche.base.service.model.DWaybillOilPrice;
import cn.huiyunche.base.service.model.DWaybillOilPriceExample;
import cn.huiyunche.base.service.vo.OrderCostTo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.interfaces.tms.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/13 上午10:33
 */
@Service
public class DWaybillOilPriceServiceImpl implements DWaybillOilPriceService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DWaybillOilPriceServiceImpl.class);

    @Autowired
    private DWaybillOilPriceMapper dWaybillOilPriceMapper;

    @Override
    public Long add(DWaybillOilPrice oilPrice) {
        LOGGER.info("add params : {}", oilPrice);
        if (null == oilPrice) {
            LOGGER.error("add params oilPrice must not be null");
            throw new IllegalArgumentException("运单油价信息不能为空");
        }
        dWaybillOilPriceMapper.insertSelective(oilPrice);

        return oilPrice.getId();
    }

    @Override
    public int update(DWaybillOilPrice oilPrice) {
        LOGGER.info("update params : {}", oilPrice);
        if (null == oilPrice) {
            LOGGER.error("update params oilPrice must not be null");
            throw new IllegalArgumentException("运单油价信息不能为空");
        }
        return dWaybillOilPriceMapper.updateByPrimaryKeySelective(oilPrice);
    }

    @Override
    public void updateByDWaybillId(Long waybillId, OrderCostTo orderCostTo) {
        LOGGER.info("updateByDWaybillId param : {}, {}", waybillId, orderCostTo);
        if (null == waybillId) {
            LOGGER.error("updateByDWaybillId param waybillId must not be null");
            throw new IllegalArgumentException("运单主键不能为空");
        }
        if (null == orderCostTo) {
            LOGGER.error("updateByDWaybillId param orderCostTo must not be null");
            throw new IllegalArgumentException("费用信息不能为空");
        }

        DWaybillOilPrice oilPrice = this.getByDWaybillId(waybillId);
        if (null != oilPrice) {
            oilPrice.setDriverOilarPrice(orderCostTo.getOilarprice());
            oilPrice.setPetrolStationOilarPrice(orderCostTo.getOilapprice());
            oilPrice.setMarketOilPrice(orderCostTo.getMarketprice());

            this.update(oilPrice);
        }
    }

    @Override
    public DWaybillOilPrice getByDWaybillId(Long waybillId) {
        LOGGER.info("getByDWaybillId params : {}", waybillId);
        if (null == waybillId) {
            LOGGER.error("getByDWaybillId param waybillId must not be null");
            throw new IllegalArgumentException("运单主键不能为空");
        }

        DWaybillOilPriceExample example = new DWaybillOilPriceExample();
        example.createCriteria().andWaybillIdEqualTo(waybillId);

        List<DWaybillOilPrice> list = dWaybillOilPriceMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }
}
